package com.zhanghe.study.java8.dll;

import com.sun.jna.Pointer;
import com.sun.jna.Structure;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;

public class TCLineInfo extends Structure {
    public static class ByReference extends TCLineInfo implements Structure.ByReference {}
    public static class ByValue extends TCLineInfo implements Structure.ByValue {}

    public int[] vgray; // 使用int数组代替std::vector<int>
    public int tpos;
    public int cpos;

    // 定义SIZE常量
    public static final int SIZE;

    static {
        // 初始化SIZE常量
        SIZE = new TCLineInfo().size();
    }

    @Override
    protected List<String> getFieldOrder() {
        return Arrays.asList("vgray", "tpos", "cpos");
    }

    public TCLineInfo() {
        super();
    }

    public TCLineInfo(int[] vgray, int tpos, int cpos) {
        this.vgray = vgray;
        this.tpos = tpos;
        this.cpos = cpos;
    }

    // 从Pointer读取数据
    public void read(Pointer ptr) {
        ByteBuffer buffer = ptr.getByteBuffer(0, SIZE);
        int vgraySize = buffer.getInt(); // 假设vgray的大小是第一个字段
        vgray = new int[vgraySize];
        for (int i = 0; i < vgraySize; i++) {
            vgray[i] = buffer.getInt();
        }
        tpos = buffer.getInt();
        cpos = buffer.getInt();
    }

    // 将数据写入Pointer
    public void write(Pointer ptr) {
        ByteBuffer buffer = ptr.getByteBuffer(0, SIZE);
        buffer.putInt(vgray.length); // 写入vgray的大小
        for (int value : vgray) {
            buffer.putInt(value);
        }
        buffer.putInt(tpos);
        buffer.putInt(cpos);
    }
}